﻿package utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import Function.InitAll;




public class JDBCUtil {

	private static String driverName = "com.mysql.jdbc.Driver";// jdbc4.0以后不需要
	private static String url = "jdbc:mysql://localhost:3306/user";
	
	private static String userName = InitAll.dbUserName;
	private static String password = InitAll.dbPassword;

	public static Connection getConnection() {
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Connection conn = null;
		//System.out.println("正在连接数据库...");
		try {
			conn = DriverManager.getConnection(url, userName, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println("数据库已连接!");
		return conn;

	}

	public static void registerDriver() {
		try {
			Class.forName(driverName);// 驱动注册
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.out.println("找不到驱动");
		}
	}



	public static void closeConnection(Connection conn) {
		//System.out.println("正在释放所有资源...");
		
			if (conn != null) {
				try {
					conn.close();
					conn = null;
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		
	}
	
	
	/*
	 * 释放所有资源
	 */
	public static void realeaseAll(ResultSet rs,Statement st,Connection conn){
		if(rs!=null){
			try {
				rs.close();
				rs = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (st!=null){
			try {
				st.close();
				st = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		closeConnection(conn);
	}
	public static void main(String[] args) {
//		User a=new User(2016,"7856","fei4","abcdd","66667");
//		ServerUserDao programme=new ServerUserDaoJDBCImpl();
//		programme.checkUser(a);
		
	
		
	}
}
